package br.com.virtualsoluction.utilitarios;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionFactory {
 
    private String sgdb;
    private String driver;
    private String host;
    private String porta;
    private String banco;
    private String usuario;
    private String senha;

    public ConnectionFactory(){
        loadConfig();
    }

    private void loadConfig() {
        
        Configuracao c = new Configuracao();
        c.lerArquivo();
        
        this.banco = c.getBanco();
        this.driver = c.getDriver();
        this.host = c.getHost();
        this.porta = c.getPorta();
        this.senha = c.getSenha();
        this.sgdb = c.getSgdb();
        this.usuario = c.getUsuario();
    }
	 
    public Connection getConnection() {

        Connection c = null;
        try{            
            String url;
            url = this.sgdb+":"+this.host+":"+this.banco;
            //url = "jdbc:firebirdsql:localhost:D:\\Faculdade\\IFTM\\ENSOF2\\ProjetoECommerce\\dataBase\\bdvs.fdb";

            Class.forName(this.driver);            
            c = DriverManager.getConnection(url, this.usuario, this.senha);
            
        }catch(ClassNotFoundException eDriver){            
            System.out.println("Driver do banco de dados nao localizado."+eDriver);
        }catch(SQLException e){            
            System.out.println("Erro ao tentar conectar com o banco de dados: "+e);
        }finally{
            return c;
        }
    }
	 
    public boolean closeConnection(Connection c) {

        try{
            c.close();
            return true;
        }catch(SQLException ex){
            return false;            
        }
    }

    public String exibeInfo(){
        String a;
        a = this.banco+" "+this.driver+" "+this.host+" "+this.porta+" "+this.senha+" "+this.sgdb+" "+this.usuario;
        return a;
    }
}